
local addonName, IWonew = ...

WayToolClass = {}
WayToolClass.__index = WayToolClass


function WayToolClass:new()
  local instance = setmetatable({}, WayToolClass)
  instance.textFrame = nil
	return instance
end

function WayToolClass:OnFrameUpdate()
  self:UpdatePos()
end

function WayToolClass:UpdatePos()
  local x, y, facing = self:GetPos()
  self:SetFrameText(x, y, facing)
end

function WayToolClass:GetPos()
  local map = C_Map.GetBestMapForUnit("player")
  if not map then return end
  local position = C_Map.GetPlayerMapPosition(map, "player")
  if not position then return end
  local facing = GetPlayerFacing()
  local x, y = position:GetXY()
  local scale = 100000000000
  return math.floor(x * scale), math.floor(y * scale), math.floor(facing * scale)
end

function WayToolClass:SetFrameText(x, y, face)
  local textStr = ""..tostring(x).."\n"..tostring(y).."\n"..tostring(face)
  if not self.textFrame then
    -- 创建一个框架
    local frame = CreateFrame("Frame", "MyBlackSquareFrame", UIParent)
    frame:SetSize(200, 80)
    frame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 640, 0)  -- 注意Y坐标是负值
    -- 设置背景颜色为黑色
    frame.texture = frame:CreateTexture()
    frame.texture:SetColorTexture(0, 0, 0, 1)  -- RGBA: 黑色不透明
    frame.texture:SetAllPoints(frame)
    -- 创建白色文字
    frame.text = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    frame.text:SetPoint("CENTER", frame, "CENTER", 0, 0)
    frame.text:SetText(textStr)  -- 设置你要显示的文本
    frame.text:SetTextColor(1, 1, 1, 1)  -- RGBA: 白色不透明
    local fontPath, _, flags = frame.text:GetFont()
    frame.text:SetFont(fontPath, 20, flags)  -- 只改字号为20
    self.textFrame = frame
  else
    self.textFrame.text:SetText(textStr)
  end
  
end

function WayToolClass:debug()
  local uiWidth, uiHeight = UIParent:GetWidth(), UIParent:GetHeight()
  print(uiWidth, uiHeight)  -- 输出示例: 1920, 1080
  local x, y, face = self:GetPos()
  local textStr = "x="..tostring(x).."\ny="..tostring(y).."\nf="..tostring(face)
  IWonew.Log(textStr)
end

local WayTool_instance = WayToolClass:new()

function WayToolClass_Debug()
  WayTool_instance:debug()
end

local updateFrame = CreateFrame("Frame")
updateFrame:SetScript("OnUpdate", function(self)
    WayTool_instance:OnFrameUpdate()
end)

